package com.senlingspace.system.service.impl;

import java.util.List;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.senlingspace.common.utils.DateUtils;
import com.senlingspace.common.utils.PageUtils;
import com.senlingspace.system.domain.vo.ProductResponse;
import com.senlingspace.system.domain.vo.WeChatProductAttributeVo;
import com.senlingspace.system.mapper.SlkjProductAttributeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.senlingspace.system.mapper.SlkjShopProductMapper;
import com.senlingspace.system.domain.SlkjShopProduct;
import com.senlingspace.system.service.ISlkjShopProductService;

/**
 * 门店商品状态Service业务层处理
 *
 * @author SenlingSpace
 * @date 2024-07-30
 */
@Service
public class SlkjShopProductServiceImpl implements ISlkjShopProductService {
    @Autowired
    private SlkjShopProductMapper slkjShopProductMapper;
    @Autowired
    private SlkjProductAttributeMapper productAttributeMapper;


    @Override
    public List<ProductResponse> getProductsByShopAndName(Long shopId, String productName, Long productTpyeId, Long productId) {
        PageUtils.startPage();
        List<ProductResponse> productResponses = slkjShopProductMapper.queryProductsByShopAndName(shopId, productName, productTpyeId, productId);
        for (ProductResponse productResponse : productResponses) {
            List<WeChatProductAttributeVo> weChatProductAttributeVos = productAttributeMapper.selectWCProductAttributeByProductId(productResponse.getProductId());
            productResponse.setProductAttributeList(weChatProductAttributeVos);
        }
        return productResponses;
    }

    /**
     * 查询门店商品状态
     *
     * @param id 门店商品状态主键
     * @return 门店商品状态
     */
    @Override
    public SlkjShopProduct selectSlkjShopProductById(Long id) {
        return slkjShopProductMapper.selectSlkjShopProductById(id);
    }

    /**
     * 查询门店商品状态列表
     *
     * @param slkjShopProduct 门店商品状态
     * @return 门店商品状态
     */
    @Override
    public List<SlkjShopProduct> selectSlkjShopProductList(SlkjShopProduct slkjShopProduct) {
        return slkjShopProductMapper.selectSlkjShopProductList(slkjShopProduct);
    }

    /**
     * 新增门店商品状态
     *
     * @param slkjShopProduct 门店商品状态
     * @return 结果
     */
    @Override
    public int insertSlkjShopProduct(SlkjShopProduct slkjShopProduct) {
        slkjShopProduct.setCreateTime(DateUtils.getNowDate());
        return slkjShopProductMapper.insertSlkjShopProduct(slkjShopProduct);
    }

    /**
     * 修改门店商品状态
     *
     * @param slkjShopProduct 门店商品状态
     * @return 结果
     */
    @Override
    public int updateSlkjShopProduct(SlkjShopProduct slkjShopProduct) {
        slkjShopProduct.setUpdateTime(DateUtils.getNowDate());
        return slkjShopProductMapper.updateSlkjShopProduct(slkjShopProduct);
    }

    /**
     * 批量删除门店商品状态
     *
     * @param ids 需要删除的门店商品状态主键
     * @return 结果
     */
    @Override
    public int deleteSlkjShopProductByIds(Long[] ids) {
        return slkjShopProductMapper.deleteSlkjShopProductByIds(ids);
    }

    /**
     * 删除门店商品状态信息
     *
     * @param id 门店商品状态主键
     * @return 结果
     */
    @Override
    public int deleteSlkjShopProductById(Long id) {
        return slkjShopProductMapper.deleteSlkjShopProductById(id);
    }

    @Override
    public List<ProductResponse> getProductTypesByShopId(Long shopId) {
        return slkjShopProductMapper.getProductTypesByShopId(shopId);
    }

    @Override
    public List<ProductResponse> getWxProductTypes() {
        return slkjShopProductMapper.getWxProductTypes();
    }

    @SuppressWarnings("resource")
    @Override
    public PageInfo<ProductResponse> getWxProductsByShopAndName(String productName, Long productTpyeId, Long productId, Integer pageNum, Integer pageSize) {
        Page<Object> page = PageUtils.simpleStartPage(pageNum, pageSize, true);
        List<ProductResponse> productResponses = slkjShopProductMapper.getWxProductsByShopAndName(productName, productTpyeId, productId);
        long total = page.getTotal();

        for (ProductResponse productResponse : productResponses) {
            List<WeChatProductAttributeVo> weChatProductAttributeVos = productAttributeMapper.selectWCProductAttributeByProductId(productResponse.getProductId());
            productResponse.setProductAttributeList(weChatProductAttributeVos);
        }

        PageInfo<ProductResponse> pageInfo = new PageInfo<>(productResponses);
        pageInfo.setTotal(total);
        pageInfo.setPageNum(page.getPageNum());
        pageInfo.setPageSize(page.getPageSize());
        return pageInfo;
    }

}
